package com.njkwt.business.system.controller;

import com.njkwt.business.system.dto.UserDto;
import com.njkwt.business.system.dto.PageDto;
import com.njkwt.business.system.dto.ResponseDto;
import com.njkwt.business.system.service.UserService;
import com.njkwt.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* @ClassNamedasda
* @Description
* @Author ydj (171799383@qq.com)
* @Date2020/8/18 10:14
* @Version V1.0
**/

@RestController
@RequestMapping("/api/business/system/user")
public class UserController {

    private static final Logger LOG = LoggerFactory.getLogger(UserController.class);
    public static final String BUSINESS_NAME = "用户";

    @Resource
    private UserService userService;

    /**
     * 列表查询
     */
    @PostMapping("/list")
    public ResponseDto list(@RequestBody PageDto pageDto) {
        ResponseDto responseDto = new ResponseDto();
        userService.list(pageDto);
        responseDto.setContent(pageDto);
        return responseDto;
    }
    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody UserDto userDto) {
        // 保存校验
        ValidatorUtil.require(userDto.getId(), "流水号");
        ValidatorUtil.length(userDto.getId(), "流水号", 1, 50);
        ValidatorUtil.require(userDto.getAccount(), "用户登录帐号");
        ValidatorUtil.length(userDto.getAccount(), "用户登录帐号", 1, 255);
        ValidatorUtil.require(userDto.getPassword(), "密码");
        ValidatorUtil.length(userDto.getPassword(), "密码", 1, 255);
        ValidatorUtil.require(userDto.getName(), "用户姓名");
        ValidatorUtil.length(userDto.getName(), "用户姓名", 1, 255);
        ValidatorUtil.require(userDto.getSex(), "性别");
        ValidatorUtil.require(userDto.getStatus(), "用户状态");
        ValidatorUtil.require(userDto.getBizcode(), "业务对照码");
        ValidatorUtil.length(userDto.getBizcode(), "业务对照码", 1, 255);
        ValidatorUtil.require(userDto.getCreatetime(), "经办时间");
        ValidatorUtil.length(userDto.getCreateid(), "创建人", 1, 50);
        ValidatorUtil.length(userDto.getTypename(), "分类名称", 1, 20);
        ValidatorUtil.length(userDto.getTypeid(), "分类ID", 1, 50);

        ResponseDto responseDto = new ResponseDto();
        userService.save(userDto);
        responseDto.setContent(userDto);
        return responseDto;
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();
        userService.delete(id);
        return responseDto;
    }
}
